package com.changgou.controller;

import com.changgou.goods.pojo.Brand;
import com.changgou.service.BrandService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 2019/12/8
 * zjh
 */
@RestController
@RequestMapping("brand")
@CrossOrigin // 跨域
public class BrandController {

    @Autowired
    private BrandService brandService;
    /**
     * 查询所有品牌
     */

    @GetMapping
    public Result<List<Brand>> findAll() {
        List<Brand> brands = brandService.findAll();
        return new Result<>(true, StatusCode.OK, "查询品牌集合成功",brands);
    }

    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable Integer id) {
        Brand brand = brandService.findById(id);
        return new Result<>(true, StatusCode.OK, "查询品牌成功",brand);
    }

    @PostMapping
    public Result add(@RequestBody Brand brand) {
        brandService.add(brand);
        return new Result(true, StatusCode.OK, "添加品牌成功");
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable Integer id, @RequestBody Brand brand) {
        brand.setId(id);
        brandService.update(brand);
        return new Result(true, StatusCode.OK, "修改品牌成功");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        brandService.delete(id);
        return new Result(true, StatusCode.OK, "删除品牌成功");
    }

    @PostMapping("search")
    public Result<List<Brand>> findList(@RequestBody Brand brand) {
        List<Brand> list = brandService.findList(brand);
        return new Result<>(true, StatusCode.OK, "条件搜索查询品牌成功", list);
    }

    @GetMapping("search/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(@PathVariable Integer page,@PathVariable Integer size) {
        PageInfo<Brand> brands = brandService.findPage(page, size);
        return new Result<>(true, StatusCode.OK, "分页查询品牌成功", brands);
    }

    @PostMapping("search/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(@PathVariable Integer page,
                                            @PathVariable Integer size,
                                            @RequestBody Brand brand) {
        PageInfo<Brand> brands = brandService.findPage(brand, page, size);
        return new Result<>(true, StatusCode.OK, "分页带条件搜索查询品牌成功", brands);
    }
}
